# -*- coding: utf-8 -*-
'''
Created on Nov 3, 2011

@author: onetera
'''
from PyQt4 import QtCore , QtGui
from assetMaker_UI import assetMaker_UI
from lib.papyDB import papyDB ,tableInfo
from lib.papyDBTable import *



class cAssetMaker( assetMaker_UI ):
    def __init__(self):
        assetMaker_UI.__init__(self)
        self.setUI()
        self.makeAsset_btn.clicked.connect(self.regAsset)        

    def regAsset(self):
        prjCodeName = str( self.prj_cb.currentText() ) 
#        PROJECT = tableInfo('PROJECT' , prjCodeName = prjCodeName )
#        SEQUENCE = tableInfo('SEQUENCE' , prjCodeName = prjCodeName)
#        SHOT = tableInfo('SHOT' , prjCodeName = prjCodeName )        
        seqID = False
        shotID = False
        
        prjID = papyDB.getID("PROJECT", "prjID", "prjCodeName", str( self.prj_cb.currentText() )  )
        if str( self.seq_cb.currentText() ) != '--Master--':
            seqID = SEQUENCE.search('seqID' , seqName=str( self.seq_cb.currentText() ))
                        
        if str( self.shot_cb.currentText() ) != '--Global--':
            shotID = SHOT.search('shotID' , shotName=str( self.shot_cb.currentText() ))         
          
        assetType = str( self.assetType_CBox.currentText() )
        dept = str( self.assignDept_CBox.currentText() )
        user = str( self.assignUser_CBox.currentText() )
        userID = int( papyDB.getFetch('select userID from USER_INFO where userName = "%s"' % user )[0][0] )
        assetName = str( self.assetName_line.text() )
        strtDate = str( self.startDate_line.text() )
        endDate = str( self.endDate_line.text() )
        assetTags = str( self.assetTag_line.text() ).split(',') if str( self.assetTag_line.text() ) !='' else []
        assetDesc = str(self.desc_txtEdt.toPlainText() )        
        if strtDate == '':            
            self.alertWin("Error" , "Please insert start date.")
            return 
        if endDate == '':            
            self.alertWin("Error" , "Please insert end date.")
            return
        if assetName=='':
            self.alertWin("Error" , "Please insert Asset Name.")
            return
        
        papyDB.regTable('ASSET' , userID , assetName , 1 ,strtDate,endDate,assetType,1,1,'0000-00-00',assetDesc)
        lastAssetID= papyDB.cr.lastrowid
        # Tagging        
        if len(assetTags) > 0 :            
            for x in assetTags:
                if not x in [k[0] for k in  papyDB.getFetch('select Tag from ASSET_TAG')] :
                    papyDB.regTable('ASSET_TAG' , x ) 
#                    lastTagID = papyDB.cr.lastrowid
                tagID = papyDB.getID('ASSET_TAG', 'assetTagID', 'Tag', x )                
                papyDB.regTable('ASSET_has_ASSET_TAG' , lastAssetID , tagID )
        
        papyDB.regTable('PROJECT_has_ASSET' ,  prjID , lastAssetID  )        
        if seqID:                 
            papyDB.regTable('SEQUENCE_has_ASSET' , seqID ,lastAssetID )
        if shotID:
            papyDB.regTable('SHOT_has_ASSET' , shotID , lastAssetID )                
        self.alertWin("Notice" , "Successfully Registered!")
        


if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    cAssetMaker = cAssetMaker()  
#    papyDB.regTable('ASSET_has_ASSET_TAG' , 4 ,5  )
    app.exec_()
    
    
    
    